
package com.nuedu.corejava.jdbc1;
/**
 * 设计一个jdbc操作工具类，用于封装通用操作
 * @author admin
 *
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBHelper {
	//封装4个参数
	private static final String DRIVER= "oracle.jdbc.driver.OracleDriver";
	private static final String URL = "jdbc:oracle:thin://localhost:1521/orcl";
	private static final String USER= "jdbcuser";
	private static final String PASSWORD= "123456";
	
	//1.注册驱动2.建立连接
	public static Connection openConnection() {
		Connection conn = null;
		try {
			Class.forName(DRIVER);
			conn = DriverManager.getConnection(URL,USER,PASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
		
			e.printStackTrace();
		}
		
		return conn;
		
		
	}
	//6.关闭连接，释放资源
		public static void closeAll(ResultSet rs,Statement stmt,Connection conn) {
			//这个一个标准的关闭动作
			if(rs!=null) {
				try {
					rs.close();
				} catch (SQLException e) {
				}
			}
			if(stmt!=null) {
				try {
					stmt.close();
				} catch (SQLException e) {
				}
			}
			
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
}
